package com.zzyl.nursing.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备相关Controller
 *
 * @author ruoyi
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询产品详情
     */
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation(value = "查询产品详情")
    public AjaxResult queryProduct(@PathVariable String productKey) {
      List<ServiceCapability> result = deviceService.queryProduct(productKey);
        return success(result);
    }
    /**
     * 查询设备相关列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursing:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备相关列表")
    public TableDataInfo<Device> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 同步产品列表
     */
    @PostMapping("/syncProductList")
    @ApiOperation("同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 获取产品列表
     */
    @GetMapping("/allProduct")
    @ApiOperation("获取产品列表")
    public AjaxResult allProduct() {
        List<ProductVo> result = deviceService.allProduct();
        return AjaxResult.success(result);
    }

    /**
     * 设备注册(添加)
     */
    @PostMapping("/register")
    @ApiOperation("设备注册(添加)")
    public AjaxResult register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return success();
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详细信息")
    public AjaxResult getDeviceDetail(@PathVariable String iotId) {
        DeviceDetailVo result = deviceService.getDeviceDetail(iotId);
        return success(result);
    }

    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报数据")
    public AjaxResult queryServiceProperties(@PathVariable String iotId) {
        List<Map<String, Object>> result = deviceService.queryServiceProperties(iotId);
        return success(result);
    }

    @PutMapping
    @ApiOperation("修改设备")
    public AjaxResult update(@RequestBody DeviceDto dto) {
        deviceService.updateDevice(dto);
        return success();
    }

    @DeleteMapping("{iotId}")
    @ApiOperation("删除设备")
    public AjaxResult remove(@PathVariable String iotId) {
        deviceService.deleteDeviceById(iotId);
        return success();
    }


//    /**
//     * 导出设备相关列表
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:nursing:export')")
//    @Log(title = "设备相关", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ApiOperation("导出设备相关列表")
//    public void export(HttpServletResponse response, Device device)
//    {
//        List<Device> list = deviceService.selectDeviceList(device);
//        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
//        util.exportExcel(response, list, "设备相关数据");
//    }
//
//    /**
//     * 获取设备相关详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:nursing:query')")
//    @GetMapping(value = "/{id}")
//    @ApiOperation("获取设备相关详细信息")
//    public R<Device> getInfo(@ApiParam(value = "设备相关ID", required = true)
//            @PathVariable("id") Long id)
//    {
//        return R.ok(deviceService.selectDeviceById(id));
//    }
//
//    /**
//     * 新增设备相关
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:nursing:add')")
//    @Log(title = "设备相关", businessType = BusinessType.INSERT)
//    @PostMapping
//    @ApiOperation("新增设备相关")
//    public AjaxResult add(@ApiParam(value = "设备相关实体", required = true) @RequestBody Device device)
//    {
//        return toAjax(deviceService.insertDevice(device));
//    }
//
//    /**
//     * 修改设备相关
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:nursing:edit')")
//    @Log(title = "设备相关", businessType = BusinessType.UPDATE)
//    @PutMapping
//    @ApiOperation("修改设备相关")
//    public AjaxResult edit(@ApiParam(value = "设备相关实体", required = true) @RequestBody Device device)
//    {
//        return toAjax(deviceService.updateDevice(device));
//    }
//
//    /**
//     * 删除设备相关
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:nursing:remove')")
//    @Log(title = "设备相关", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    @ApiOperation("删除设备相关")
//    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids)
//    {
//        return toAjax(deviceService.deleteDeviceByIds(ids));
//    }
}
